import { ref, onMounted, onUnmounted } from 'vue'
import COMMONAPI from '@/api/modules/common' // API
import { useOutsideUserInfoStore } from '@/store/userInfo' // store
const useInfoStore = useOutsideUserInfoStore()
import constData from '@/api/const.js' // 常量数据

// 从别的已授权页面进入时不需要查询是否授权
function userLogin(callback) {
    // 定义的方法和参数
    const loadingStatus = ref(null)
    const isDelFlag = ref(false) // 会员是否冻结
    const phone = ref('') // 手机号
    const showphone = ref('') // 展示的带***手机号

    let params = {
        openId: useInfoStore.openId,
        appId: useInfoStore.appId,
        token: useInfoStore.userToken || '',
        apptype: constData.wxType
    }
    COMMONAPI.API_CRM_LOGIN_GET(params).then((res) => {
        loadingStatus.value = false
        let result = res.data || {}
        let token = result.token || ''
        // isDel- 会员是否冻结
        if (result.isDel == 'Y') {
            isDelFlag.value = true
            useInfoStore.setSingleAttrValue('userToken', '')
            useInfoStore.setSingleAttrValue('isDelFlag', true)
        } else {
            isDelFlag.value = false
            useInfoStore.setSingleAttrValue('userToken', token)
            useInfoStore.setSingleAttrValue('isDelFlag', false)
        }
        phone.value = useInfoStore.userInfo.phoneNumner
            ? useInfoStore.userInfo.phoneNumner
            : result.mobile
        useInfoStore.setSingleAttrValue('phoneNumner', phone.value)
        showphone.value = phone.value ? phone.value.substr(0, 3) + '****' + phone.value.substr(7) : ''
        callback()
    })
    return {
        loadingStatus,
        isDelFlag,
        phone,
        showphone
    }
}
// CRM登陆 + 授权查询
function crmLogin(callback) {
    // 定义的方法和参数
    const loadingStatus = ref(null)
    const isDelFlag = ref(false) // 会员是否冻结
    const phone = ref('') // 手机号
    const showphone = ref('') // 展示的带***手机号

    let params = {
        openId: useInfoStore.openId,
        appId: useInfoStore.appId,
        token: useInfoStore.userToken || '',
        apptype: constData.wxType
    }
    COMMONAPI.API_CRM_LOGIN_GET(params).then((res) => {
        let result = res.data || {}
        let token = result.token || ''
        // isDel- 会员是否冻结
        if (result.isDel == 'Y') {
            isDelFlag.value = true
            useInfoStore.setSingleAttrValue('userToken', '')
            useInfoStore.setSingleAttrValue('isDelFlag', true)
        } else {
            isDelFlag.value = false
            useInfoStore.setSingleAttrValue('userToken', token)
            useInfoStore.setSingleAttrValue('isDelFlag', false)
        }
        callback()
        COMMONAPI.API_IS_AUTHED_GET({ token: token }).then((res) => {
            loadingStatus.value = false
            if (res.status == 'S' && res.data && res.data.errcode == '304') {
                useInfoStore.setSingleAttrValue('isCRMUser', false)
            } else if (res.status == 'S' && res.data && res.data.errcode == '200') {
                phone.value = res.data.errmsg || ''
                useInfoStore.setSingleAttrValue('isCRMUser', true)
                useInfoStore.setSingleAttrValue('phoneNumner', phone.value)
                showphone.value = phone.value
                    ? phone.value.substr(0, 3) + '****' + phone.value.substr(7)
                    : ''
            } else {
                useInfoStore.setSingleAttrValue('isCRMUser', false)
            }
        })
    })
    return {
        loadingStatus,
        isDelFlag,
        phone,
        showphone
    }
}
export default {
    userLogin,crmLogin
}


